package org.chen.ctrip.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.chen.common.ctrip.entity.ContentReplaceRule;
import org.chen.common.result.ResultVO;
import org.chen.ctrip.service.ContentReplaceRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

// ContentReplaceRuleController.java
@RestController
@RequestMapping("/replace-rules")
@Tag(name = "不重要的接口", description = "不重要的接口")
public class ContentReplaceRuleController {

    @Autowired
    private ContentReplaceRuleService contentReplaceRuleService;

    @GetMapping
    public ResultVO getAllRules() {
        try {
            return ResultVO.success(contentReplaceRuleService.list());
        } catch (Exception e) {
            return ResultVO.fail("操作失败" + e.getMessage());
        }
    }

    @GetMapping("/enabled")
    public ResultVO getEnabledRules() {
        try {
            return ResultVO.success(contentReplaceRuleService.getEnabledRules());
        } catch (Exception e) {
            return ResultVO.fail("操作失败" + e.getMessage());
        }
    }

    @PostMapping
    public ResultVO createRule(@RequestBody ContentReplaceRule rule) {
        try {
            contentReplaceRuleService.saveRule(rule);
            return ResultVO.success("规则创建成功");
        } catch (Exception e) {
            return ResultVO.fail("操作失败" + e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public ResultVO updateRule(@PathVariable Long id, @RequestBody ContentReplaceRule rule) {
        try {
            contentReplaceRuleService.updateRule(rule);
            return ResultVO.success("规则更新成功");
        } catch (Exception e) {
            return ResultVO.fail("操作失败" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ResultVO deleteRule(@PathVariable Long id) {
        try {
            contentReplaceRuleService.deleteRule(id);
            return ResultVO.success("规则删除成功");
        } catch (Exception e) {
            return ResultVO.fail("操作失败" + e.getMessage());
        }
    }

    @PutMapping("/{id}/toggle")
    public ResultVO toggleRuleStatus(@PathVariable Long id, @RequestParam Boolean enabled) {
        try {
            contentReplaceRuleService.toggleRuleStatus(id, enabled);
            return ResultVO.success("规则状态更新成功");
        } catch (Exception e) {
            return ResultVO.fail("操作失败" + e.getMessage());
        }
    }

    @PostMapping("/test")
    public ResultVO testReplaceRules(@RequestBody Map<String, String> request) {
        Map<String, Object> result = new HashMap<>();
        try {
            String content = request.get("content");
            String processedContent = contentReplaceRuleService.applyReplaceRules(content);
            result.put("original", content);
            result.put("processed", processedContent);
            return ResultVO.success("规则测试成功", result);
        } catch (Exception e) {
            return ResultVO.fail("操作失败" + e.getMessage());
        }
    }
}